package com.tgpms.web.hongbo.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.interfaces.dto.HongBoInterface;
import com.tgpms.util.Common;
import com.tgpms.web.hongbo.dao.TubeDocumentMapper;
import com.tgpms.web.hongbo.entity.TubeDocument;
import com.tgpms.web.hongbo.service.ITubeDocumentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jl
 * @since 2020-10-26
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
public class TubeDocumentServiceImpl implements ITubeDocumentService {

	@Resource
	private HongBoInterface hongBoInterface;

	@Resource
	private TubeDocumentMapper mapper;

	@Override
	public int syncTubeDocument() throws UnsupportedEncodingException, ParseException {
		int returnInt;
		List<TubeDocument> list = new ArrayList<>();
		int ifEmpty = mapper.deleteTubeDocument();
		if (ifEmpty != -1) {
			//  查询处理宏波数据批量插入
			Result tubeResult = hongBoInterface.getTubeDocument(-1, -1);
			if (tubeResult.getSuccess()) {
				String resData = tubeResult.getData().toString();
				JSONObject resJson = JSONObject.parseObject(resData);
				JSONArray tables = (JSONArray) resJson.get("tables");
				for (Object table : tables) {
					JSONObject jsonObject = JSONObject.parseObject(table.toString());
					TubeDocument temp = transform(jsonObject);
					list.add(temp);
				}
			}
		}
		returnInt = mapper.batchInsert(list);
		return returnInt;
	}

	/**
		新增
	*/
	@Override
	public Result addTubeDocument(TubeDocument en,Result result) {
		String id = Common.fromDateFormats()+UUID.randomUUID().toString().replaceAll("-", "");
		en.setId(id);
		en.setRowstamp(id);
		en.setInsertDate(new Date());
		int i = mapper.insert(en);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_INSERT.getExplain());
			result.setData(id);
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}
	
	/**
		更新
	*/
	@Override
	public Result updateTubeDocument(TubeDocument en,Result result) {
		int i = mapper.updateById(en);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_UPDATE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}
	
	/**
		根据主键删除
	*/
	@Override
	public Result deleteTubeDocument(Serializable id,Result result) {
		int i = mapper.deleteById(id);
		if(i==1) {
			result.setMsg(ExceptionExplain.SUCCESS_BY_DELETE.getExplain());
		}else {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}

	private TubeDocument transform(JSONObject jsonObject) throws ParseException {
		TubeDocument temp = new TubeDocument();
		temp.setId(jsonObject.get("ID").toString());
		temp.setFkey(jsonObject.get("F_Key").toString());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		temp.setCreateTime(sdf.parse(jsonObject.get("CreateTime").toString()));
		temp.setLatitude(jsonObject.get("Latitude").toString());
		temp.setUploader(jsonObject.get("Uploader").toString());
		temp.setLongitude(jsonObject.get("Longitude").toString());
		temp.setPartId(jsonObject.get("PartID").toString());
		temp.setUrl(jsonObject.get("url").toString());
		temp.setInsertUser("TGPMS");
		temp.setInsertDate(new Date());
		temp.setRowstamp(jsonObject.get("ID").toString());
		return temp;
	};
}
